/*
 优化记录
 * */
define([ '../../common/service'], function (http) {
    var OptRecords = {
        init: function () {
            var _this = this;
            //初始化tab
            $("div.ndp-tab-wrapper[name=plain-tab]").tabs({
                list:[I18N.getText("rulescriptList","roleListTitle"),
                    I18N.getText("rulescriptResult","ruleResult"),
                    I18N.getText("optRecords","optRecords"),
                    I18N.getText("rulescriptSetting","setting")]
            }).on("TAB_CHANGE",function(e){//点击其中的一个tab发生事件
                var href = "";
                switch (e.originalEvent.data.index){
                    case 0:
                        href = "#!rulescriptList";
                        break;
                    case 1:
                        href = "#!rulescriptResult";
                        break;
                    case 2:
                        href = "#!optRecords";
                        break;
                    case 3:
                        href = "#!rulescriptSetting";
                        break;
                }
                window.location.hash = href;
            }).val(2);//设置选中的tab 索引，默认0;

            //列表管理
            $('[list-manager="opt-record-list"]').listManager('init', {
                isRealTime:true
                ,setTopVal:165//调整吸顶的高度,需要减去这个高度
                ,scrollDOM:$('.fmp-article')//监听滚动条
                ,supportAjaxPage: true		//是否支持配置列表ajxa分页[多列表两种状态都有时，统一配置为true,禁用的列表pageJson参数为空即可]
                ,pageCallback:function(query){
                    _this._initList(query);
                }
            }, function(query){
                _this._initList(query);
                //_this._optRetry();
                //_this._optExecute();
            });
        },
        _initList:function(_query){
            var _this = this;
            _this.timezoneValue = window.sessionStorage.timezoneValue;
            //遮罩
            var loadingArea = $('.container-fluid');
            Kit.loading.show(loadingArea);
            http.work("/ruleresult/getOptLogs",_query,'POST').then(function(res){
                var _table = $('table[list-manager="opt-record-list"]'),
                    _tbody = $('tbody', _table),
                    tmpHtml = '',
                    _list = res.data.logs;

                if(_list==undefined){
                    console.log('not has record');
                }else{
                    for (var i = 0; i < _list.length; i++) {
                        tmpHtml += _this. _generateHTML(_list[i], i + 1);
                    }
                    _tbody.html(tmpHtml);
                    _table.listManager([{'resetTd':false, 'resetPageData': res.data.paginator}]);
                }
                //隐藏遮罩
                Kit.loading.hide(loadingArea);
            });

        },
        _generateHTML:function(obj,index){
            var _this = this;
            _this.timezoneValue = window.sessionStorage.timezoneValue;
            var tmpHtml="";
            tmpHtml += '<tr>' + '<td>' + (index) + '</td>';
            tmpHtml += '<td>' + obj.fbAccountId +  '</td>';
            tmpHtml += '<td title="'+ obj.connectionObjectName + '" class="text-left">' + obj.connectionObjectName +  '</td>';
            tmpHtml += '<td title="'+ obj.platform + '" class="text-left">' + obj.platform +  '</td>';
            //adSetName
            tmpHtml += '<td title="'+ obj.adSetName + '" class="text-left">' + obj.adSetName +  '</td>';

            //版本
            tmpHtml += '<td title="'+ obj.campaignGroupName + '" class="text-left">' + obj.campaignGroupName + '</td>';
            tmpHtml += '<td title="'+ obj.desc + '"  class="text-left">' + obj.desc + '</td>';
            var updateDate = moment(obj.optTime).utcOffset(_this.timezoneValue).format('YYYY-MM-DD HH:mm:ss');
            tmpHtml += '<td title="'+updateDate + '"  class="text-center">' +updateDate + '</td>';

            tmpHtml += '</tr>';
            return tmpHtml;
        }
    };
    return OptRecords;
});
